﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.NPOIOperateExcel;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormShatterSumUp : FormTemplet
    {
        private ShatterCondition _shatterCondition = null;

        private DataTable _dtSumUp = null;


        public FormShatterSumUp()
        {
            this.InitializeComponent();
        }

        public FormShatterSumUp(ShatterCondition sc) : this()
        {
            this._shatterCondition = sc;
            Shatters shatters = new Shatters();
            try
            {
                this._dtSumUp = shatters.GetSumUpByCondition(this._shatterCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            shatters.Dispose();
            if (this._dtSumUp != null)
            {
                if (this._dtSumUp.Rows.Count > 0)
                {
                    this.dgSumUp.DataSource = null;
                    this.dgSumUp.DataSource = this._dtSumUp;
                }
                else
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n订单项信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgSumUp.DataSource = null;
                }
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (this.dgSumUp.Rows.Count < 1)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                int rowIndex = this.dgSumUp.CurrentCell.RowIndex;
                new FormShatterInfo(this.dgSumUp.Rows[rowIndex].Cells[1].Value.ToString())
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void btnShowTotal_Click(object sender, EventArgs e)
        {
            if (this._dtSumUp != null)
            {
                if (this._dtSumUp.Rows.Count < 1)
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    int num = 0;
                    decimal d = 0m;
                    decimal d2 = 0m;
                    for (int i = 0; i < this._dtSumUp.Rows.Count; i++)
                    {
                        num += Convert.ToInt32(Regex.IsMatch(this._dtSumUp.Rows[i][3].ToString(), "^[0-9]+|([0-9]+\\.[0-9]+)$") ? this._dtSumUp.Rows[i][3].ToString() : "0");
                        d += Convert.ToDecimal(Regex.IsMatch(this._dtSumUp.Rows[i][4].ToString(), "^[0-9]+|([0-9]+\\.[0-9]+)$") ? this._dtSumUp.Rows[i][4].ToString() : "0");
                        d2 += Convert.ToDecimal(this._dtSumUp.Rows[i][5]);
                    }
                    decimal num2 = d2 * 40m;
                    this.txtResult.Text = string.Concat(new string[]
                    {
                        "补片数量：",
                        num.ToString(),
                        "   补片面积：",
                        d.ToString(),
                        "   换算面积：",
                        d2.ToString(),
                        "   扣除金额：",
                        num2.ToString()
                    });
                }
            }
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.Filter = "Excel文件|*.xls";
            this.saveFileDialog1.Title = "请选择你要保存的路径和文件名";
            this.saveFileDialog1.FileName = "补片单列表_Report.xls";
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = this.saveFileDialog1.FileName;
                ExportShatterList exportShatterList = null;
                try
                {
                    exportShatterList = new ExportShatterList(Application.StartupPath + "\\model\\" + AppSettings.GetExcelSettings("general"), true);
                    exportShatterList.MakeDataToExcel(fileName, this._dtSumUp);
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n或者是: 和你要创建的相同文件名的目标文件正在使用，不能覆盖，请把它关闭或删除后在操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                if (exportShatterList != null)
                {
                    exportShatterList.Dispose();
                }
            }
        }

        private void btnShowTypeSubtotal_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.Show(this, new Point(this.btnShowTypeSubtotal.Left, this.btnShowTypeSubtotal.Bottom));
        }

        private void menuShowTypeSubtotal_1_Click(object sender, EventArgs e)
        {
            Shatters shatters = new Shatters();
            DataTable dataTable = null;
            try
            {
                dataTable = shatters.GetTypeSubtotalByCondition(this._shatterCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            shatters.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    string text = string.Concat(new string[]
                    {
                        "※ 补片单的分类信息如下：\r\n\r\n     ",
                        CommonMethod.PadSpaceOnRight("工作组", 10),
                        CommonMethod.PadSpaceOnRight("成品名称", 22),
                        CommonMethod.PadSpaceOnRight("补片层名称", 16),
                        CommonMethod.PadSpaceOnRight("补片数量", 12),
                        CommonMethod.PadSpaceOnRight("补片面积", 10),
                        "\r\n"
                    });
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        string text2 = text;
                        text = string.Concat(new string[]
                        {
                            text2,
                            "\r\n",
                            (i + 1).ToString().PadRight(3, ' '),
                            "、",
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i]["工作组"].ToString(), 10),
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i]["成品名称"].ToString(), 22),
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i]["补片层名称"].ToString(), 16),
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i]["补片数量"].ToString(), 12),
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i]["补片面积"].ToString(), 10)
                        });
                    }
                    text = "《补片单工作组_类型总结》".PadLeft(45, ' ') + "\r\n\r\n" + text;
                    if (ContentBox.ShowBox("补片单工作组_类型总结", text, "是否保存这些数据 ?") == DialogResult.Yes)
                    {
                        this.saveFileDialog1.Filter = "Excel文件|*.xls";
                        this.saveFileDialog1.Title = "请选择你要保存的路径和文件名";
                        this.saveFileDialog1.FileName = "补片单工作组_类型总结_Report.xls";
                        if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            string fileName = this.saveFileDialog1.FileName;
                            General general = null;
                            try
                            {
                                general = new General(Application.StartupPath + "\\model\\" + AppSettings.GetExcelSettings("general"), true);
                                general.MakeDataToExcel(fileName, dataTable);
                            }
                            catch (IOException ex3)
                            {
                                MessageBox.Show(ex3.Message + "\r\n或者是: 和你要创建的相同文件名的目标文件正在使用，不能覆盖，请把它关闭或删除后在操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            if (general != null)
                            {
                                general.Dispose();
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("补片单中可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            if (dataTable != null)
            {
                dataTable.Dispose();
            }
        }

        private void menuShowTypeSubtotal_2_Click(object sender, EventArgs e)
        {
            Shatters shatters = new Shatters();
            DataTable dataTable = null;
            try
            {
                dataTable = shatters.GetTypeSubtotalByCondition_2(this._shatterCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            shatters.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    string text = "※ 补片单的分类信息如下：\r\n";
                    decimal d = 0m;
                    string text2;
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        d += Convert.ToDecimal(dataTable.Rows[i][5]);
                        text2 = text;
                        text = string.Concat(new string[]
                        {
                            text2,
                            "\r\n",
                            (i + 1).ToString().PadRight(3, ' '),
                            "、工作组：",
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i][0].ToString(), 10),
                            "面积：",
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i][1].ToString(), 10),
                            "补片厚度：",
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i][2].ToString() + "mm", 10),
                            "补片总面积：",
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i][3].ToString(), 10),
                            "扣薪面积：",
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i][4].ToString(), 10),
                            "折合面积：",
                            CommonMethod.PadSpaceOnRight(dataTable.Rows[i][5].ToString(), 10)
                        });
                    }
                    decimal num = d * 40m;
                    text2 = text;
                    text = string.Concat(new string[]
                    {
                        text2,
                        "\r\n\r\n     ★总折合面积：",
                        d.ToString(),
                        "；   总折合金额：",
                        num.ToString()
                    });
                    text = "《补片单工作组总结》".PadLeft(45, ' ') + "\r\n\r\n" + text;
                    if (ContentBox.ShowBox("补片单工作组总结", text, "是否保存这些数据 ?") == DialogResult.Yes)
                    {
                        this.saveFileDialog1.Filter = "Text文件|*.txt";
                        this.saveFileDialog1.Title = "请选择你要保存的路径和文件名";
                        this.saveFileDialog1.FileName = "补片单工作组总结.txt";
                        if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            string fileName = this.saveFileDialog1.FileName;
                            this.saveFileDialog1.Dispose();
                            try
                            {
                                OrderSystem.SystemFramework.TextWriter.WriteText(fileName, text);
                            }
                            catch (Exception ex2)
                            {
                                MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                        else
                        {
                            this.saveFileDialog1.Dispose();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("补片单中可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            if (dataTable != null)
            {
                dataTable.Dispose();
            }
        }

        private void FormShatterSumUp_Load(object sender, EventArgs e)
        {
            this.dgSumUp.AutoResizeColumns();
        }

        private void dgSumUp_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            new FormShatterInfo(this.dgSumUp.Rows[e.RowIndex].Cells[1].Value.ToString())
            {
                MdiParent = base.MdiParent
            }.Show();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
